package com.dylan.历史.java8.lambda表达式.练习题;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Dylan on 2018/7/19.
 */
public class 第2题 {

    public static void main(String[] args) {
        File[] subDirs = listSubDirectory3(new File("/Users/ankang/Downloads"));
        Arrays.asList(subDirs).stream().map(File::getName).forEach(System.out::println);
    }

    // 使用匿名内部类
    static File[] listSubDirectory1(File dir) {
        if (!dir.isDirectory()) {
            throw new RuntimeException();
        }
        return dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory();
            }
        });
    }

    // 使用lambda表达式
    static File[] listSubDirectory2(File dir) {
        if (!dir.isDirectory()) {
            throw new RuntimeException();
        }
        return dir.listFiles(f -> f.isDirectory());
    }

    // 使用方法引用
    static File[] listSubDirectory3(File dir) {
        if (!dir.isDirectory()) {
            throw new RuntimeException();
        }
        return dir.listFiles(File::isDirectory);
    }
}
